package tiepbm.app.store.adapter;

import java.util.ArrayList;

import tiepbm.app.store.Global;
import tiepbm.app.store.activity.R;
import tiepbm.app.store.object.Obj_App;
import tiepbm.app.util.lazyLoader.ImageLoader_nghiank;
import tiepbm.app.util.lazyLoader.MyItem_img_nghiank;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class HorizontalListViewAdapter extends ArrayAdapter<Obj_App> {

	ArrayList<Obj_App> mItems = new ArrayList<Obj_App>();
	LayoutInflater mInflater;
	private Activity mContext;
//	private MyItem_img_nghiank mItem_img3 = null;

	public HorizontalListViewAdapter(Context mContext, ArrayList<Obj_App> mItems) {
		super(mContext, R.layout.listview_items_vertical, mItems);
		mInflater = LayoutInflater.from(mContext);
		this.mContext = (Activity) mContext;
		this.mItems = mItems;
	}

	@Override
	public Obj_App getItem(int position) {
		// TODO Auto-generated method stub
		return mItems.get(position);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mItems.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = null;
		MyItem_img_nghiank mItem_img3 = null;
		if (convertView == null) {
			LayoutInflater inflater = mContext.getLayoutInflater();
			v = inflater.inflate(R.layout.listview_items_vertical, null);

			ViewHolder holder = new ViewHolder();

			holder.imgIcon = (ImageView) v.findViewById(R.id.imvIcon);
			holder.lblName = (TextView) v.findViewById(R.id.tvTitle);
			// holder.lblDeveloper = (TextView)
			// v.findViewById(R.id.tvDeveloper);
			holder.rbRate = (RatingBar) v.findViewById(R.id.rbRate);
			// holder.lblMoney = (TextView) v.findViewById(R.id.tvMoney);
			v.setTag(holder);
		} else {
			v = convertView;
		}
		ViewHolder holder = (ViewHolder) v.getTag();
		holder.lblName.setText(mItems.get(position).getName());
		// holder.lblDeveloper.setText(mItems.get(position).getmDeveloper());
		ImageView image = holder.imgIcon;
		holder.imgIcon.setImageDrawable(getContext().getResources()
				.getDrawable(R.drawable.stub));
		// if(mItems.get(position).getmType()==0){
		// holder.lblMoney.setText(R.string.mienphi);
		// }else{
		// holder.lblMoney.setText(mItems.get(position).getmMoney());
		// }
		if (mItems.get(position).getRate() > 0)
			holder.rbRate.setRating(mItems.get(position).getRate());

		mItem_img3 = new MyItem_img_nghiank(mContext, image);
		try {
			ImageLoader_nghiank.getInstance().displayImage(
					mItems.get(position).getIcon(), mItem_img3);

		} catch (OutOfMemoryError e) {
			Global.getInstance().clearCache();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return v;
	}

	static class ViewHolder {
		private ImageView imgIcon;
		private TextView lblName;
		private RatingBar rbRate;

	}
}
